#include "Settings.hh"

//ClassImp(Settings);

Settings::Settings(G4String settings, Int_t VerboseLevel, Int_t Cut, Bool_t RotateMiniball)
{
  int i,j;
  cout<<"start reading "<<settings<<" ... \n";
  TEnv* env = new TEnv(settings);

  fBadStripsFile = env->GetValue("BadStripsFile","/Users/JacobSJ/Physics/Phd./is430/2009/Calibration/BadStrips.dat");
  TargetMaterialName = env->GetValue("Target.Material.Name","0.9PE");
  TargetMaterialZ = env->GetValue("Target.Material.Z", 0);
  TargetMaterialMole = env->GetValue("Target.Material.Mole", 0);
  TargetMaterialMole *= g/mole;
  TargetMaterialDensity = env->GetValue("Target.Material.Density", 0.92);
  TargetMaterialDensity *= g/cm3;
  TargetThickness = env->GetValue("Target.Thickness", 0.92);
  TargetThicknessInmgPercm2 = TargetThickness;
  TargetThickness *= mg/cm2;
  TargetRadius = env->GetValue("Target.Radius", 5);
  TargetRadius *= mm;     
  TargetAngle = env->GetValue("Target.Angle", 90.);
  TargetAngle *= deg;     
  TargetShift[0] = env->GetValue("Target.Shift.x", 0.);
  TargetShift[0] *= mm;     
  TargetShift[1] = env->GetValue("Target.Shift.y", 0.);
  TargetShift[1] *= mm;     
  TargetShift[2] = env->GetValue("Target.Shift.z", 1.);
  TargetShift[2] *= mm;     
  BeamAngle = env->GetValue("Beam.Angle",0.);
  BeamAngle *= degree;
  BeamDirection = env->GetValue("Beam.Direction",0.);
  BeamDirection *= degree;
  MiniballNb = env->GetValue("NumberOfMiniballCluster", 8);
  if(MiniballNb > 8)
    {
      cerr<<"maximum of eight miniball cluster supported, please change the file "<<settings<<endl;

      exit(2);
    }
  for(i=0;i<MiniballNb;i++)
    {
      MiniballType[i] = env->GetValue(Form("Miniball.Type.%d",i),3);
      MiniballRho[i] = env->GetValue(Form("Miniball.Rho.%d",i),100.);  
      MiniballRho[i] *= mm;
      MiniballRho[i] += MiniBallTripleDetector::GetDistanceToFront()*mm;
      MiniballTheta[i] = env->GetValue(Form("Miniball.Theta.%d",i),0.);
      MiniballTheta[i] *= deg;
      MiniballPhi[i] = env->GetValue(Form("Miniball.Phi.%d",i),0.); 
      MiniballPhi[i] *= deg; 
      MiniballSpin[i] = env->GetValue(Form("Miniball.Spin.%d",i),0.); 
      MiniballSpin[i] *= deg;
      MiniballName[i] = env->GetValue(Form("Miniball.Name.%d",i),"default"); 
      if(env->GetValue(Form("Miniball.RelativeEfficiency.%d",i),0.) != 0.)
	{
	  for(j = 0; j < 3; j++)
	    {
	      MiniballRelativeEfficiency[i][j] = env->GetValue(Form("Miniball.RelativeEfficiency.%d",i),0.);
	    }
	}
      else
	{
	  for(j = 0; j < 3; j++)
	    {
	      MiniballRelativeEfficiency[i][j] = env->GetValue(Form("Miniball.RelativeEfficiency.%d.%d",i,j),0.);
	    }
	}
    }

  NumberOfCDSectors = env->GetValue("NumberOfCDSectors",4);
  NumberOfCDRings = env->GetValue("NumberOfCDRings",16);
  NumberOfCDStrips = env->GetValue("NumberOfCDStrips",24);

  SSDInnerRad = env->GetValue("CD.InnerRadius",7.5); 
  SSDInnerRad *= mm;
  SSDOuterRad = env->GetValue("CD.OuterRadius",42.5); 
  SSDOuterRad *= mm;
  SSDPadOuterRad = env->GetValue("CDE.OuterRadius",51.0); 
  SSDPadOuterRad *= mm;
  for(i = 0; i < 4; i++)
    {
      fSSDThickness[i] = env->GetValue(Form("CD.Forward.Thickness.%d",i),500.); 
      fSSDThickness[i] *= micrometer;
      bSSDThickness[i] = env->GetValue(Form("CD.Backward.Thickness.%d",i),500.); 
      bSSDThickness[i] *= micrometer;
      feSSDThickness[i] = env->GetValue(Form("CDE.Forward.Thickness.%d",i),1000.); 
      feSSDThickness[i] *= micrometer;
      beSSDThickness[i] = env->GetValue(Form("CDE.Backward.Thickness.%d",i),500.); 
      beSSDThickness[i] *= micrometer;
    }
  SSDZPosition = env->GetValue("CD.Z-Position",63.);
  SSDZPosition *= mm;
  SSDDeadLayer = env->GetValue("CD.DeadLayer",0.7);
  SSDDeadLayer *= micrometer;
  SSDAngle = env->GetValue("CD.Angle",82.);     
  SSDAngle *= deg;
  SSDRotation = env->GetValue("CD.Rotation",0.);     
  SSDRotation *= deg;
//  SSDRotation -= 131.*deg; //// why ????
  CDEZPosition = env->GetValue("CDE.Z-Position",65.);     
  CDEZPosition *= mm;

  SSDActInnerRad = env->GetValue("CD.ActiveArea.InnerRadius",9.);
  SSDActInnerRad *= mm;
  SSDActOuterRad = env->GetValue("CD.ActiveArea.OuterRadius",40.9);
  SSDActOuterRad *= mm;
  SSDPadActOuterRad = env->GetValue("CDE.ActiveArea.OuterRadius",50.0);
  SSDPadActOuterRad *= mm;
  SSDActLength = env->GetValue("CD.ActiveArea.Length",31.9);
  SSDActLength *= mm;
  SSDSegAngle = env->GetValue("CD.Segment.Angle",81.6); 
  SSDSegAngle *= deg;
  SSDAnnPitch = env->GetValue("CD.AnnularPitch",2.); 
  SSDAnnPitch *= mm;
  SSDAnnWidth = env->GetValue("CD.AnnularWidth",1.9); 
  SSDAnnWidth *= mm;
  SSDRadPitch = env->GetValue("CD.RadialPitch",3.4); 
  SSDRadPitch *= deg;

  SSDPCBOuterRad = env->GetValue("CD.PCB.OuterRadius",61.0);
  SSDPCBOuterRad *= mm;
  SSDPCBInnerRad = env->GetValue("CD.PCB.InnerRadius",7.4);
  SSDPCBInnerRad *= mm;
  SSDPCBStripThickness = env->GetValue("CD.PCB.Thickness",2.0);
  SSDPCBStripThickness *= mm;
  SSDPCBPadThickness = env->GetValue("CDE.PCB.Thickness",2.0);
  SSDPCBPadThickness *= mm;

  //barrel detectors
  ForwardBarrelZ = env->GetValue("Barrel.Forward.Z-Position",33.0); 
  ForwardBarrelZ *= mm;
  BackwardBarrelZ = env->GetValue("Barrel.Backward.Z-Position",-33.0); 
  BackwardBarrelZ *= mm;
  BarrelDeltaEGap = env->GetValue("Barrel.DeltaEGap",1.7); 
  BarrelDeltaEGap *= mm;
  for(i = 0; i < 4; i++)
    {
      fBarrelThickness[i] = env->GetValue(Form("Barrel.Forward.Thickness.%d",i),140.); 
      fBarrelThickness[i] *= micrometer;
      bBarrelThickness[i] = env->GetValue(Form("Barrel.Backward.Thickness.%d",i),500.); 
      bBarrelThickness[i] *= micrometer;
      feBarrelThickness[i] = env->GetValue(Form("Barrel.Forward.Pad.Thickness.%d",i),1000.); 
      feBarrelThickness[i] *= micrometer;
      beBarrelThickness[i] = env->GetValue(Form("Barrel.Backward.Pad.Thickness.%d",i),1000.); 
      beBarrelThickness[i] *= micrometer;
    }
  BarrelLength = env->GetValue("Barrel.Length",52.); 
  BarrelLength *= mm;
  BarrelWidth = env->GetValue("Barrel.Width",52.); 
  BarrelWidth *= mm;
  BarrelActLength = env->GetValue("Barrel.ActiveArea.Length",50.); 
  BarrelActLength *= mm;
  BarrelActWidth = env->GetValue("Barrel.ActiveArea.Width",50.); 
  BarrelActWidth *= mm;
  BarrelStripPitch = env->GetValue("Barrel.StripPitch",3.125); 
  BarrelStripPitch *= mm;
  ChannelsPerBarrelDetector = env->GetValue("ChannelsPerBarrelDetector",17); 
  BarrelDisplacement = env->GetValue("Barrel.Displacement",16.5); 
  BarrelDisplacement *= mm;
  BarrelScaling = env->GetValue("Barrel.Scaling",1.); 
  BarrelDistance = env->GetValue("Barrel.DistanceToBeam",29.);
  BarrelDistance *= mm; 

  //epoxy board of barrel detectors
  PCBLength = env->GetValue("Barrel.PCB.Length",54.); 
  PCBLength *= mm;
  PCBWidth = env->GetValue("Barrel.PCB.Width",89.); 
  PCBWidth *= mm;
  ePCBWidth = env->GetValue("Barrel.PCB.Pad.Width",78.); 
  ePCBWidth *= mm;
  PCBThickness = env->GetValue("Barrel.PCB.Thickness",1.5); 
  PCBThickness *= mm;
  PCBRecessLength = env->GetValue("Barrel.PCB.Recess.Length",22.); 
  PCBRecessLength *= mm;
  PCBRecessWidth = env->GetValue("Barrel.PCB.Recess.Width",23.); 
  PCBRecessWidth *= mm;

  //chamber settings
  ChamberWallThickness = env->GetValue("Chamber.WallThickness",2.); 
  ChamberWallThickness *= mm;
  ChamberInnerRadius = env->GetValue("Chamber.InnerRadius",98.); 
  ChamberInnerRadius *= mm;
  ChamberLength = env->GetValue("Chamber.Length",70.); 
  ChamberLength *= mm;
  ChamberFrontThickness = env->GetValue("Chamber.FrontThickness",10.); 
  ChamberFrontThickness *= mm;
  ChamberBackThickness = env->GetValue("Chamber.BackThickness",2.); 
  ChamberBackThickness *= mm;

  ChamberFlangeConnectionThickness = env->GetValue("Chamber.Flange.ConnectionThickness",3.);
  ChamberFlangeConnectionThickness *= mm;
  ChamberForwardFlangeInnerRadius = env->GetValue("Chamber.Flange.Forward.InnerRadius",20.); 
  ChamberForwardFlangeInnerRadius *= mm;
  ChamberForwardFlangeOuterRadius = env->GetValue("Chamber.Flange.Forward.OuterRadius",40.); 
  ChamberForwardFlangeOuterRadius *= mm;
  ChamberForwardFlangeLength = env->GetValue("Chamber.Flange.Forward.Length",10.); 
  ChamberForwardFlangeLength *= mm;
  ChamberForwardFlangeConnectionLength = env->GetValue("Chamber.Flange.Forward.ConnectionLength",10.); 
  ChamberForwardFlangeConnectionLength *= mm;
  ChamberBackwardFlangeInnerRadius = env->GetValue("Chamber.Flange.Backward.InnerRadius",70.); 
  ChamberBackwardFlangeInnerRadius *= mm;
  ChamberBackwardFlangeOuterRadius = env->GetValue("Chamber.Flange.Backward.OuterRadius",85.); 
  ChamberBackwardFlangeOuterRadius *= mm;
  ChamberBackwardFlangeLength = env->GetValue("Chamber.Flange.Backward.Length",10.); 
  ChamberBackwardFlangeLength *= mm;
  ChamberBackwardFlangeConnectionLength = env->GetValue("Chamber.Flange.Backward.ConnectionLength",30.); 
  ChamberBackwardFlangeConnectionLength *= mm;
  ChamberTargetFlangeInnerRadius = env->GetValue("Chamber.Flange.Target.InnerRadius",20.25); 
  ChamberTargetFlangeInnerRadius *= mm;
  ChamberTargetFlangeOuterRadius = env->GetValue("Chamber.Flange.Target.OuterRadius",27.5); 
  ChamberTargetFlangeOuterRadius *= mm;
  ChamberTargetFlangeLength = env->GetValue("Chamber.Flange.Target.Length",3.); 
  ChamberTargetFlangeLength *= mm;
  ChamberTargetFlangeConnectionLength = env->GetValue("Chamber.Flange.Target.ConnectionLength",27.); 
  ChamberTargetFlangeConnectionLength *= mm;


  //screening foil settings
  CDFoilInnerRadius = env->GetValue("CD.Foil.InnerRadius",8.);
  CDFoilInnerRadius *= mm;
  CDFoilOuterRadius = env->GetValue("CD.Foil.OuterRadius",41.);
  CDFoilOuterRadius *= mm;
  CDFoilDistance = env->GetValue("CD.Foil.Distance",1.);
  CDFoilDistance *= mm;
  FCDFoilThickness = env->GetValue("CD.Forward.Foil.Thickness",50.);
  FCDFoilThickness *= micrometer;
  BCDFoilThickness = env->GetValue("CD.Backward.Foil.Thickness",2.);
  BCDFoilThickness *= micrometer;

  BDFoilDistance = env->GetValue("Barrel.Foil.Distance",1.);
  BDFoilDistance *= mm;
  FBDFoilThickness = env->GetValue("Barrel.Forward.Foil.Thickness",12.);
  FBDFoilThickness *= micrometer;
  BBDFoilThickness = env->GetValue("Barrel.Backward.Foil.Thickness",12.);
  BBDFoilThickness *= micrometer;
  BBDFoilHole = env->GetValue("Barrel.Foil.Vertical.HoleRadius",3.);
  BBDFoilHole *= mm;
  BBDVertFoilDistance = env->GetValue("Barrel.Foil.Vertical.Distance",5.);
  BBDVertFoilDistance *= mm;
  BBDVertFoilSize = env->GetValue("Barrel.Foil.Vertical.Size",58.);
  BBDVertFoilSize *= mm;

  delete env;

  TEnv *badstrips = new TEnv((char*)this->BadStripsFile());
  cout << "BadStripsFile" << (char*)this->BadStripsFile() << endl;

  for(int j=0;j<4;j++){
    for(int i=0;i<16;i++){
      fBadStrips[0][j][i] = badstrips->GetValue(Form("Forward.%d.%d",j,i),0);
      if(fBadStrips[0][j][i]==1)
	cout << "Bad Strip Forward " << j << " Strip " << i << endl;
      fBadStrips[1][j][i] = badstrips->GetValue(Form("Backward.%d.%d",j,i),0);
      if(fBadStrips[1][j][i]==1)
	cout << "Bad Strip Backward " << j << " Strip " << i << endl;

    }
  }


  
  if(MiniballNb == 0)
    {
      cout<<"ERROR in Settings: MiniballNb is zero!\n\n";
    }

  //for bit 2 in Cut set, select only phi between 90 und 270 degree (behind visible part of chamber)
  if(Cut&(1<<1))
    {
      for(i=0;i<MiniballNb;i++)
	{
	  if(MiniballPhi[i]/degree < 360. || MiniballPhi[i]/degree > 0.)
	    {
	      if(VerboseLevel > 0)
		{
		  cout<<"rejecting MiniballTheta["<<i<<"] = "<<MiniballTheta[i]/degree<<endl;
		  cout<<"rejecting MiniballPhi["<<i<<"] = "<<MiniballPhi[i]/degree<<endl;
		  cout<<"rejecting MiniballSpin["<<i<<"] = "<<MiniballSpin[i]/degree<<endl;
		}
	      for(j=i+1;j<MiniballNb;j++) //copy remaining values into lower array positions
		{
		  MiniballTheta[j-1] = MiniballTheta[j];
		  MiniballPhi[j-1] = MiniballPhi[j];
		  MiniballSpin[j-1] = MiniballSpin[j];
		}
	      if(MiniballPhi[i]/degree < 360. || MiniballPhi[i]/degree > 0.) //if first copied value is also to be rejected, check it again
		{
		  i--;
		}
	      MiniballNb--;
	    }
	  else
	    {
	      if(VerboseLevel > 0)
		{
		  cout<<"accepting MiniballTheta["<<i<<"] = "<<MiniballTheta[i]/degree<<endl;
		  cout<<"accepting MiniballPhi["<<i<<"] = "<<MiniballPhi[i]/degree<<endl;
		  cout<<"accepting MiniballSpin["<<i<<"] = "<<MiniballSpin[i]/degree<<endl;
		}
	    }
	}
    }
  //MiniballNb =0;
  //rotate MiniballTheta and Phi
  if(RotateMiniball)
    {
      for(i=0;i<MiniballNb;i++)
	{
	  // check if phi > PI -> rotation angles change
	  if(MiniballPhi[i]/rad>M_PI) {
	    MiniballPhi[i] = 2*M_PI*rad - MiniballPhi[i];
	    MiniballTheta[i] = -MiniballTheta[i];
	  }
	  
	  TVector3 trans;
	  trans.SetXYZ(1,0,0);
	  trans.RotateY(-MiniballPhi[i]/rad);
	  trans.RotateX(-MiniballTheta[i]/rad);
	  
	  MiniballTheta[i]=trans.Theta()*rad;
	  MiniballPhi[i]=trans.Phi()*rad;
	  // done transforming theta and phi
	  
	  // find alpha offset
  
	  TVector3 vd,va,vn;
	  const TVector3 ex(1,0,0);
  
	  vd.SetXYZ(0,0,1);
	  va.SetXYZ(1,0,0);
  
	  vd.RotateY(MiniballTheta[i]/deg);
	  vd.RotateZ(MiniballPhi[i]/deg);
	  va.RotateY(MiniballTheta[i]/deg);
	  va.RotateZ(MiniballPhi[i]/deg);
  
	  vn=ex-vd*(ex*vd);
	  vn.SetMag(1.);

	  double spat=vn*(va.Cross(vd));
	  double alpha_offset=va.Angle(vn);  //*spat;
	  if (spat<0) alpha_offset*=-1;
	  MiniballSpin[i] = -MiniballSpin[i]-alpha_offset*rad;
	}
    }

  if(VerboseLevel > 0)
    {
      cout<<"read following Settings:\n"
	  <<"Target:\t"<<TargetRadius/mm<<"*mm\t"<<TargetThickness/(mg/cm2)<<" mg/cm2\t = "
	  <<TargetThickness/TargetMaterialDensity/micrometer<<" um"<<endl;
      cout<<"MiniBall-Type:\t";
      for(i=0;i<MiniballNb;i++)
	cout<<MiniballType[i]<<" ";
      cout<<endl;
      cout<<"MiniBall-Rho:\t";
      for(i=0;i<MiniballNb;i++)
	cout<<MiniballRho[i]/mm<<" ";
      cout<<endl;
      cout<<"MiniBall-Theta:\t";
      for(i=0;i<MiniballNb;i++)
	cout<<MiniballTheta[i]/deg<<" ";
      cout<<endl;
      cout<<"MiniBall-Phi:\t";
      for(i=0;i<MiniballNb;i++)
	cout<<MiniballPhi[i]/deg<<" ";
      cout<<endl;
      cout<<"MiniBall-Spin:\t";
      for(i=0;i<MiniballNb;i++)
	cout<<MiniballSpin[i]/deg<<" ";
      cout<<endl;
      cout<<"MiniBall-Name:\t";
      for(i=0;i<MiniballNb;i++)
	cout<<MiniballName[i]<<" ";
      cout<<endl;
      cout<<"CD: "<<endl
	  <<"NumberOfCDSectors = "<<NumberOfCDSectors<<endl
	  <<"NumberOfCDRings = "<<NumberOfCDRings<<endl
	  <<"NumberOfCDStrips = "<<NumberOfCDStrips<<endl
	  <<"SSDInnerRad = "<<SSDInnerRad/mm<<" mm"<<endl
	  <<"SSDOuterRad = "<<SSDOuterRad/mm<<" mm"<<endl
	  <<"SSDPadOuterRad = "<<SSDPadOuterRad/mm<<" mm"<<endl;
      for(i = 0; i < 4; i++)
	{
	  cout<<"fSSDThickness["<<i<<"] = "<<fSSDThickness[i]/micrometer<<" um"<<endl
	      <<"bSSDThickness["<<i<<"] = "<<bSSDThickness[i]/micrometer<<" um"<<endl
	      <<"feSSDThickness["<<i<<"] = "<<feSSDThickness[i]/micrometer<<" um"<<endl
	      <<"beSSDThickness["<<i<<"] = "<<beSSDThickness[i]/micrometer<<" um"<<endl;
	}
      cout<<"SSDZPosition = "<<SSDZPosition/mm<<" mm"<<endl
	  <<"SSDDeadLayer = "<<SSDDeadLayer/micrometer<<" um"<<endl
	  <<"SSDAngle = "<<SSDAngle/deg<<" deg"<<endl
	  <<"SSDRotation = "<<SSDRotation/deg<<" deg"<<endl
	  <<"CDEZPosition = "<<CDEZPosition/mm<<" mm"<<endl
	  <<"SSDActInnerRad = "<<SSDActInnerRad/mm<<" mm"<<endl
	  <<"SSDActOuterRad = "<<SSDActOuterRad/mm<<" mm"<<endl
	  <<"SSDPadActOuterRad = "<<SSDPadActOuterRad/mm<<" mm"<<endl
	  <<"SSDActLength = "<<SSDActLength/mm<<" mm"<<endl
	  <<"SSDSegAngle = "<<SSDSegAngle/deg<<" deg"<<endl
	  <<"SSDAnnPitch = "<<SSDAnnPitch/mm<<" mm"<<endl
	  <<"SSDAnnWidth = "<<SSDAnnWidth/mm<<" mm"<<endl
	  <<"SSDRadPitch = "<<SSDRadPitch/deg<<" deg"<<endl
	  <<"SSDPCBOuterRad = "<<SSDPCBOuterRad/mm<<" mm"<<endl
	  <<"SSDPCBInnerRad = "<<SSDPCBInnerRad/mm<<" mm"<<endl
	  <<"SSDPCBStripThickness = "<<SSDPCBStripThickness/mm<<" mm"<<endl
	  <<"SSDPCBPadThickness = "<<SSDPCBPadThickness/mm<<" mm"<<endl;
      
      cout<<"Barrel: "<<endl
	  <<"ForwardBarrelZ = "<<ForwardBarrelZ/mm<<" mm"<<endl
	  <<"BackwardBarrelZ = "<<BackwardBarrelZ/mm<<" mm"<<endl
	  <<"BarrelDeltaEGap = "<<BarrelDeltaEGap/mm<<" mm"<<endl
	  <<"fBarrelThickness = "<<fBarrelThickness[0]/micrometer<<" um"<<endl
	  <<"bBarrelThickness = "<<bBarrelThickness[0]/micrometer<<" um"<<endl
	  <<"feBarrelThickness = "<<feBarrelThickness[0]/micrometer<<" um"<<endl
	  <<"beBarrelThickness = "<<beBarrelThickness[0]/micrometer<<" um"<<endl
	  <<"BarrelLength = "<<BarrelLength/mm<<" mm"<<endl
	  <<"BarrelWidth = "<<BarrelWidth/mm<<" mm"<<endl
	  <<"BarrelActLength = "<<BarrelActLength/mm<<" mm"<<endl
	  <<"BarrelActWidth = "<<BarrelActWidth/mm<<" mm"<<endl
	  <<"BarrelStripPitch = "<<BarrelStripPitch/mm<<" mm"<<endl
	  <<"BarrelDisplacement = "<<BarrelDisplacement/mm<<" mm"<<endl
	  <<"BarrelScaling = "<<BarrelScaling<<endl;
      cout<<"PCB board:"<<endl
	  <<"PCBLength = "<<PCBLength/mm<<" mm"<<endl
	  <<"PCBWidth = "<<PCBWidth/mm<<" mm"<<endl
	  <<"ePCBWidth = "<<ePCBWidth/mm<<" mm"<<endl
	  <<"PCBThickness = "<<PCBThickness/mm<<" mm"<<endl
	  <<"PCBRecessLength = "<<PCBRecessLength/mm<<" mm"<<endl
	  <<"PCBRecessWidth = "<<PCBRecessWidth/mm<<" mm"<<endl;
      cout<<"Chamber:"<<endl
	  <<"ChamberWallThickness = "<<ChamberWallThickness/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberInnerRadius/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberForwardFlangeInnerRadius/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberForwardFlangeOuterRadius/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberForwardFlangeLength/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberForwardFlangeConnectionLength/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberBackwardFlangeInnerRadius/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberBackwardFlangeOuterRadius/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberBackwardFlangeLength/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberBackwardFlangeConnectionLength/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberTargetFlangeInnerRadius/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberTargetFlangeOuterRadius/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberTargetFlangeLength/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberTargetFlangeConnectionLength/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberLength/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberFrontThickness/mm<<" mm"<<endl
	  <<"Chamber = "<<ChamberBackThickness/mm<<" mm"<<endl;
    }//if VerboseLevel > 0
}

void Settings::CreateBranches(TTree* tree)
{
  tree->Branch("TargetThickness",&TargetThicknessInmgPercm2,"TargetThickness/D");

  tree->Branch("fSSDThickness",&fSSDThickness,"fSSDThickness/D");
  tree->Branch("bSSDThickness",&bSSDThickness,"bSSDThickness/D");
  tree->Branch("feSSDThickness",&feSSDThickness,"feSSDThickness/D");
  tree->Branch("beSSDThickness",&beSSDThickness,"beSSDThickness/D");
  tree->Branch("SSDZPosition",&SSDZPosition,"SSDZPosition/D");
  tree->Branch("SSDRotation",&SSDRotation,"SSDRotation/D");
  tree->Branch("CDEZPosition",&CDEZPosition,"CDEZPosition/D");
  tree->Branch("SSDActInnerRad",&SSDActInnerRad,"SSDActInnerRad/D");
  tree->Branch("SSDActOuterRad",&SSDActOuterRad,"SSDActOuterRad/D");
  tree->Branch("SSDActLength",&SSDActLength,"SSDActLength/D");
  tree->Branch("SSDSegAngle",&SSDSegAngle,"SSDSegAngle/D");
  tree->Branch("SSDAnnPitch",&SSDAnnPitch,"SSDAnnPitch/D");
  tree->Branch("SSDAnnWidth",&SSDAnnWidth,"SSDAnnWidth/D");
  tree->Branch("SSDRadPitch",&SSDRadPitch,"SSDRadPitch/D");
  tree->Branch("NumberOfCDSectors",&NumberOfCDSectors,"NumberOfCDSectors/I");
  tree->Branch("NumberOfCDRings",&NumberOfCDRings,"NumberOfCDRings/I");
  tree->Branch("NumberOfCDStrips",&NumberOfCDStrips,"NumberOfCDStrips/I");
  tree->Branch("ForwardBarrelZ",&ForwardBarrelZ,"ForwardBarrelZ/D");
  tree->Branch("BackwardBarrelZ",&BackwardBarrelZ,"BackwardBarrelZ/D");
  tree->Branch("BarrelDeltaEGap",&BarrelDeltaEGap,"BarrelDeltaEGap/D");
  tree->Branch("fBarrelThickness",&fBarrelThickness,"fBarrelThickness/D");
  tree->Branch("bBarrelThickness",&bBarrelThickness,"bBarrelThickness/D");
  tree->Branch("feBarrelThickness",&feBarrelThickness,"feBarrelThickness/D");
  tree->Branch("beBarrelThickness",&beBarrelThickness,"beBarrelThickness/D");
  tree->Branch("PCBLength",&PCBLength,"PCBLength/D");
  tree->Branch("PCBWidth",&PCBWidth,"PCBWidth/D");
  tree->Branch("BarrelActLength",&BarrelActLength,"BarrelActLength/D");
  tree->Branch("BarrelActWidth",&BarrelActWidth,"BarrelActWidth/D");
  tree->Branch("BarrelStripPitch",&BarrelStripPitch,"BarrelStripPitch/D");
  tree->Branch("ChannelsPerBarrelDetector",&ChannelsPerBarrelDetector,"ChannelsPerBarrelDetector/I");
  tree->Branch("BarrelDisplacement",&BarrelDisplacement,"BarrelDisplacement/D");
  tree->Branch("BarrelScaling",&BarrelScaling,"BarrelScaling/D");
  //tree->Branch("",&,"/D");
}
